package ca.ericslandry.client.mvp.view;

import ca.ericslandry.client.GwtUtils;
import ca.ericslandry.client.mvp.presenter.HeaderPresenter;
import ca.ericslandry.client.mvp.presenter.UserPresenter;
import ca.ericslandry.client.resource.MyConstants;
import ca.ericslandry.client.resource.MyImages;
import ca.ericslandry.shared.domain.User;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

public class HeaderView extends BaseView implements HeaderPresenter.Display {
	private FlexTable flexTable = new FlexTable();
	private Hyperlink login = new Hyperlink(MyConstants.INSTANCE.login(), "");
	private Hyperlink logout = new Hyperlink(MyConstants.INSTANCE.logout(), "");
	private DockPanel panel = new DockPanel();

	public HeaderView() {
		panel.setWidth("100%");
		panel.add(new Image(MyImages.INSTANCE.logo()), DockPanel.WEST);
		panel.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
		panel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		HTML warning = new HTML(MyConstants.INSTANCE.workInProgress());
		warning.setStyleName("redAndBold");
		panel.add(warning, DockPanel.CENTER);
		panel.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);
		flexTable.setWidget(0, 0, new Image(MyImages.INSTANCE.yourgroup()));
		flexTable.setWidget(0, 3, login);
		panel.add(flexTable, DockPanel.EAST);
	}

	@Override
	public HasClickHandlers getLogin() {
		return login;
	}

	@Override
	public HasClickHandlers getLogout() {
		return logout;
	}

	@Override
	public void showLogin() {
		// Replace Gravatar image with default one
		flexTable.setWidget(0, 0, new Image(MyImages.INSTANCE.yourgroup()));
		// Remove user's display name
		flexTable.clearCell(0, 1);
		// Remove spacer
		flexTable.clearCell(0, 2);
		flexTable.setWidget(0, 3, login);
	}

	@Override
	public void showLogout() {
		flexTable.setWidget(0, 3, logout);
	}

	@Override
	public void showUser(User user) {
		if (user == null) {
			Window.alert("user is null");
		} else {
			showAvatar(user.getEmail().getEmail());
			flexTable.setWidget(0, 1, UserPresenter.getLinkViewUser(user));
			flexTable.setWidget(0, 2, new HTML(" | "));
		}
	}

	@Override
	public void showUserLoading() {
		flexTable.setWidget(0, 1, new HTML(MyConstants.INSTANCE.processingLogin()));
	}

	/**
	 * @see <a href="http://en.gravatar.com/site/implement/url">Gravatar</a>
	 */
	protected void showAvatar(String email) {
		String md5 = GwtUtils.md5(email);
		flexTable.setWidget(0, 0,
				new HTML("<img src=\"http://www.gravatar.com/avatar/" + md5 + "?s=32&d=identicon\"/>"));
	}

	@Override
	public Widget asWidget() {
		return panel;
	}
}
